package com.xrui.hbase.codec;

import com.google.common.base.Preconditions;
import com.moz.kiji.delegation.Lookups;
import com.moz.kiji.delegation.NamedProvider;
import com.xrui.hbase.RowKeyComponents;
import com.xrui.hbase.schema.RowKeyFormat;

import java.io.IOException;


/**
 * Interface for row key decoders.
 * <p>
 * A row key decoder is specific to rows of one table and decodes only one type of value.
 *
 * <p>
 * RowKey decoders are instantiated via {@link RowKeyDecoder.Factory}.
 * <p>
 */
public interface RowKeyDecoder extends NamedProvider {
    /**
     * Decodes a row key from its binary-encoded form.
     *
     * @param bytes  Binary encoded row key.
     * @param format Format spec of the row key.
     * @return the decoded RowKeyComponents.
     * @throws IOException on I/O error.
     */
    RowKeyComponents decode(byte[] bytes, RowKeyFormat format) throws IOException;

    class Factory {
        /**
         * Creates a new RowKeyDecoder with specified decoder provider name
         * @param name of the decoder
         * @return new instance of the decoder
         */
        public static RowKeyDecoder create(String name) {
            Preconditions.checkNotNull(name);
            return Lookups.getNamed(RowKeyDecoder.class).lookup(name);
        }
    }
}
